package com.rabbiter.ordering.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbiter.ordering.common.Result;
import com.rabbiter.ordering.entity.*;
import com.rabbiter.ordering.entity.Package;
import com.rabbiter.ordering.service.DishCategoryService;
import com.rabbiter.ordering.service.FoodsService;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;

import com.rabbiter.ordering.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/foods")
public class FoodsController {

    @Resource
    FoodsService foodsService;
    @Resource
    PackageService packageService;

    @Autowired
    private DishCategoryService dishcategoryService;
    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Foods foods) {
        foodsService.add(foods);
        return Result.success();
    }
    @GetMapping("/selectAlls")
    public Result selectAlls() {
        List<Package> list = packageService.list();
        return Result.success(list);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        foodsService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        foodsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 更新
     */
    @PutMapping("/update")
    public Result update(@RequestBody Foods foods) {
        foodsService.updateById(foods);
        return Result.success();
    }

    /**
     * 查询单个
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Foods foods = foodsService.selectById(id);
        return Result.success(foods);
    }
    @GetMapping("/selectAllWithFlavors")
    public Result selectAllWithFlavors() {
        List<Foods> foods = foodsService.selectAll();

        // 获取所有分类
        Map<Integer, DishCategory> categoryMap = dishcategoryService.listAll().stream()
                .collect(Collectors.toMap(DishCategory::getId, Function.identity()));

        // 为每个菜品设置口味选项
        for (Foods food : foods) {
            if (food.getCateid() != null) {
                DishCategory category = categoryMap.get(food.getCateid());
                if (category != null && category.getFlavors() != null) {
                    try {
                        // 解析JSON格式的口味
                        ObjectMapper mapper = new ObjectMapper();
                        List<String> flavors = mapper.readValue(category.getFlavors(),
                                new TypeReference<List<String>>() {});
                        food.setFlavors(flavors);
                    } catch (Exception e) {

                        food.setFlavors(Collections.emptyList());
                    }
                }
            }
        }

        return Result.success(foods);
    }
    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Foods> list = foodsService.selectAll();
        return Result.success(list);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectPage")
    public Result selectPage(Params params) {
        PageInfo<Foods> pageInfo = foodsService.selectPage(params);
        return Result.success(pageInfo);
    }

}
